package cn.it.shop.listener;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import cn.it.shop.model.Category;
import cn.it.shop.model.Product;
import cn.it.shop.service.CategoryService;
import cn.it.shop.service.ProductService;
import cn.it.shop.util.ProductTimerTask;

/**
 * @ClassName: InitDataListener
 * @Description: TODO(用于项目启动的时候项目初始化)
 * @author zsl
 * @date 2016-9-24 下午10:40:13
 * 
 */
public class InitDataListener implements ServletContextListener {

	private ApplicationContext context = null;
	private ProductTimerTask productTimerTask = null;

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {

	}

	@Override
	public void contextInitialized(ServletContextEvent event) {
		// 获取业务逻辑类，查询商品信息
		context = WebApplicationContextUtils.getWebApplicationContext(event
				.getServletContext());
		productTimerTask = (ProductTimerTask) context.getBean("productTimerTask");
		//将application传入productTimerTask
		productTimerTask.setApplication(event.getServletContext());
		// 通过设置定时器让首页的数据每隔一段时间刷新一次(守护线程)
		new Timer(true).schedule(productTimerTask, 0, 60000);

	}

}
